package com.bjpowernode;

import com.bjpowernode.handler.MyInvocationHandler;
import com.bjpowernode.service.SomeService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.reflect.Proxy;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        /*String config = "ApplicationContext.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        SomeService someService = (SomeService) context.getBean("someService");
        someService.doSome();
        System.out.println("==========================");
        someService.doOther();*/
        String config = "ApplicationContext.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        // 创建目标对象
        SomeService someService = (SomeService) context.getBean("someService");
        // 创建我们自己的InvocationHandler对象
        MyInvocationHandler myInvocationHandler = new MyInvocationHandler(someService);
        // 使用proxy创建代理对象 第一个参数:类加载器,第二个参数:实现了那些即可,
        // 第三个参数使用那个代理对象来实现增强业务
        SomeService proxy = (SomeService) Proxy.newProxyInstance(someService.getClass().getClassLoader(),
                someService.getClass().getInterfaces(),
                myInvocationHandler);
        proxy.doSome();
        System.out.println("==========================");
        proxy.doOther();

    }
}
